package com.oktest.measure.ui.fragment.work

import android.os.Bundle
import android.view.View
import com.cali.common.constant.EventSimple
import com.cali.common.kt.*
import com.cali.libcore.base.BaseFragment
import com.cali.libcore.dialog.alertdialog.helper.CommonDialogHelper
import com.cali.libcore.http.RetrofitManagement
import com.oktest.measure.MyApp
import com.oktest.measure.R
import com.oktest.measure.http.Api
import com.oktest.measure.kt.API_SUCCESS_CODE
import com.oktest.measure.kt.EVENT_HOME_TASK_GET
import com.oktest.measure.kt.TASK_DEFAULT_ID
import com.oktest.measure.kt.resultTryContext
import kotlinx.android.synthetic.main.download_layout_dialog_default.view.*
import kotlinx.android.synthetic.main.fragment_work_test_over.*
import kotlinx.coroutines.launch

/**
 * @Author hhh
 * @Date 2020/9/19
 */
class TestOverFragment :BaseFragment() {
    override fun getLayoutId(): Int {
        return R.layout.fragment_work_test_over
    }

    private val dialogHelper by lazy {
        CommonDialogHelper(context!!)
    }

    override fun initData(savedInstanceState: Bundle?) {
        toolbarHelper?.apply {
            setTitleText("调试完工")
        }
        val xml = MyApp.roomDatabase.xmlDao().load(MyApp.currentTaskId)
        tvTaskGroup.text = xml?.name
        initBtnState()
        btnCommit.setOnClickListener {
            showDialog()
        }
    }

    private fun initBtnState() {
        if (MyApp.currentTaskId == TASK_DEFAULT_ID) {
            btnCommit.gone()
        } else {
            btnCommit.visible()
        }
    }

    private fun showDialog() {
        val downloadLayout = View.inflate(context, R.layout.download_layout_dialog_default, null)
        downloadLayout?.apply {
            dialogContent.text = "确定完成该任务？"
            dialogTitle.text = "温馨提示"
            btnCancel.setOnClickListener {
                dialogHelper.dismiss()
            }
            btnOk.setOnClickListener {
                dialogHelper.dismiss()
                taskOver()
            }
        }
        dialogHelper.show(downloadLayout, false, isMath = false)
    }

    private fun taskOver() = launch {
        val complete = resultTryContext {
            val map = hashMapOf("id" to MyApp.currentTaskId)
            RetrofitManagement.instance.getService(Api::class.java)
                .taskComplete(map.jsonBody()).execute().body()
        }
        if (complete.code == API_SUCCESS_CODE) {
            MyApp.roomDatabase.homeTaskDao().apply {
                val task = query(MyApp.currentTaskId.toString())
                if (task != null) {
                    delete(task)
                }
            }
            MyApp.currentTaskId = TASK_DEFAULT_ID
            initBtnState()
            // 刷新home
            EventSimple(EVENT_HOME_TASK_GET).post()
        } else {
            error(complete)
        }
    }

    override fun hasToolbar(): Boolean {
        return true
    }

    override fun onBackPressedSupport(): Boolean {
        pop()
        return true
    }
}